<template>
  <div>
    <zbTable ref="tableRef" :api-fun="get_list" @action="action" />
    <!-- 新增/编辑弹窗 -->
    <user-edit v-if="showEdit" v-model:visible="showEdit" @done="done" />
  </div>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Modal, message } from 'ant-design-vue';
  import { useRouter } from 'vue-router';
  /* import zbTable from '@/components/zbTable/index.vue'; */
  import zbTable from '@/components/zbTable/wareIndex.vue';
  import userEdit from './components/user-edit.vue';
  import { get_list, stock_init } from '@/api/jewelry/goods_info';
  import { daochuFn } from '@/utils/daochu.js';
  const router = useRouter();
  const { push } = useRouter();
  const tableRef = ref(null);
  const showEdit = ref(false);
  const done = (data) => {
    Modal.confirm({
      title: '重要提示',
      content:
        '使用库存初始化清零，将影响跟库存有关的数据，及单据，只有销售单据不会影响，请谨慎操作！',
      okText: '确认',
      cancelText: '取消',
      onOk() {
        stock_init(data)
          .then((res) => {
            message.success(res.msg);
            setTimeout(() => {
              tableRef.value.reload();
              showEdit.value = false;
            }, 500);
          })
          .catch((e) => {
            message.error(e);
          });
      },
      onCancel() {}
    });
  };
  const action = (data) => {
    console.log('data', data);
    if (data.action_data.type == 'detail') {
      toDetail(data.row_data);
    }
    if (data.action_data.type == 'init') {
      showEdit.value = true;
    }
  };
  const toDetail = (record) => {
    let r = router.currentRoute.value.path.split('/');
    let newUrl = `/${r[1]}/${r[2]}/detail`;
    push({
      path: newUrl,
      query: { code: record.code }
    });
  };
</script>
